﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace AUV5.Web.Pages
{
    public partial class AddAccount : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    BindData();
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        #region##绑定数据
        /// <summary>
        /// 绑定数据
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void BindData()
        {
            BindAccountType();

            AUV5.Model.UserAccount account = AUV5.BLL.UserAccountBLL.GetInstance().GetUserAccountByUserId(VerifiedUserID);

            if (account != null)
            {
                txtAccount.Text = account.Account;
                txtOpenAddress.Text = account.OpenAccountAdress;
                txtPhoneNo.Text = account.PhoneNo;
                txtRealName.Text = account.RealName;
                ddlAccoutType.SelectedValue = account.AccountTypeID;
                lblId.Text = account.UserAccountID;
            }
        }
        #endregion

        #region##绑定帐号类型
        /// <summary>
        /// 绑定帐号类型
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void BindAccountType()
        {
            IList<AUV5.Model.AccountType> list = AUV5.BLL.AccountTypeBLL.GetInstance().GetAccountTypeList();

            ddlAccoutType.DataSource = list;
            ddlAccoutType.DataTextField = "AccountTypeName";
            ddlAccoutType.DataValueField = "AccountTypeID";
            ddlAccoutType.DataBind();
            ddlAccoutType.SelectedIndex = 0;
        }
        #endregion

        #region##提交
        /// <summary>
        /// 提交
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSub_Click(object sender, EventArgs e)
        {
            //if (txtAccount.Text.Trim() == "")
            //{
            //    Alert("帐号不能为空");
            //}
            if (txtRealName.Text.Trim() == "")
            {
                Alert("真实姓名不能为空");
            }
            else if (txtOpenAddress.Text.Trim() == "")
            {
                Alert("开户银行支行");
            }
            else if (txtAccount.Text.Trim() == "")
            {
                Alert("开户银行卡号不能为空");
            }
            else if (txtAccountAgain.Text == "")
            {
                Alert("请再次输入开户银行卡号");
            }
            else if (txtAccount.Text.Trim() != txtAccountAgain.Text.Trim())
            {
                Alert("开户银行卡号两次输入不一致");
            }
            else if (txtPhoneNo.Text.Trim() == "")
            {
                Alert("手机号不能为空");
            }
            else
            {
                Regex phoneRe = new Regex("^1\\d{10}$", RegexOptions.None);
                Regex accountRe = new Regex("^\\d+$");
                if (!phoneRe.IsMatch(txtPhoneNo.Text.Trim()))
                {
                    Alert("手机号格式不正确");
                }
                else if (!accountRe.IsMatch(txtAccount.Text.Trim()))
                {
                    Alert("银行卡号只能是数字");
                }
                else
                {
                    try
                    {
                        AUV5.Model.UserAccount account = AUV5.BLL.UserAccountBLL.GetInstance().GetUserAccountByUserId(VerifiedUserID);

                        if (account == null)
                        {
                            account = new Model.UserAccount();
                            account.Account = txtAccount.Text.Trim();
                            account.AccountTypeID = ddlAccoutType.SelectedValue;
                            account.OpenAccountAdress = txtOpenAddress.Text.Trim();
                            account.PhoneNo = txtPhoneNo.Text.Trim();
                            account.RealName = txtRealName.Text.Trim();
                            account.UserID = VerifiedUserID;
                            account.UserAccountID = Guid.NewGuid().ToString();

                            AUV5.BLL.UserAccountBLL.GetInstance().Add(account);
                        }
                        else
                        {
                            account.Account = txtAccount.Text.Trim();
                            account.AccountTypeID = ddlAccoutType.SelectedValue;
                            account.OpenAccountAdress = txtOpenAddress.Text.Trim();
                            account.PhoneNo = txtPhoneNo.Text.Trim();
                            account.RealName = txtRealName.Text.Trim();

                            AUV5.BLL.UserAccountBLL.GetInstance().Update(account);
                        }
                        AlertGo("编辑成功", "WithdrawList.aspx");
                    }
                    catch
                    {
                        Alert("编辑失败");
                    }
                }
            }
        }
        #endregion
    }

}